// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';
import { getToken, setToken } from '@/utils/auth';

/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(options?: { [key: string]: any }) {
  return request<{
    data: API.CurrentUser;
  }>('/api/currentUser', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 退出登录接口 POST /api/auth/system/logout */
export async function outLogin(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/auth/system/logout', {
    method: 'POST',
    ...(options || {}),
  });
}

export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  const { username, password, ...rest } = body as any;
  const transformedBody = {
    userName: username,
    userPassword: password,
    ...rest
  };

  try {
    const response = await request<API.LoginResult>('/api/auth/system/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: transformedBody,
      ...(options || {}),
    });

    // 登录成功后存储 token
    if (response.code === 200 && response.data?.access_token) {
      setToken(response.data.access_token);
    }

    return response;
  } catch (error) {
    console.error('登录请求失败:', error);
    throw error;
  }
}

/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
  return request<API.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 获取规则列表 GET /api/rule */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<API.RuleList>('/api/rule', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 更新规则 PUT /api/rule */
export async function updateRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    data: {
      method: 'update',
      ...(options || {}),
    },
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    data: {
      method: 'post',
      ...(options || {}),
    },
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/rule', {
    method: 'POST',
    data: {
      method: 'delete',
      ...(options || {}),
    },
  });
}

// 获取部门列表
export async function getDeptList() {
  return request<API.DeptListResult>('/api/sys/getSysLoginDept', {
    method: 'GET',
  });
}

// 获取岗位列表
export async function getPostList(params: { deptId: string }) {
  return request<API.PostListResult>('/api/sys/getSysLoginPosition', {
    method: 'GET',
    params,
  });
}

// 设置用户部门和岗位
export async function setUserDeptPost(body: API.UserDeptPostParams) {
  return request<API.UserDeptPostResult>('/api/sys/deptAndPositionLogin', {
    method: 'POST',
    data: body,
  });
}

// 获取用户信息
export async function getUserInfo() {
  return request('/api/sys/userInfo', {
    method: 'GET',
  });
}

export async function getresource() {
  return request('/api/sys/resource', {
    method: 'GET',
  });
}
